unit frmLibMgr;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, RzButton, StdCtrls, RzStatus, OBDirectories, siComp;

type
  TFormLibMgr = class(TForm)
    lvDll: TListView;
    btnChoose: TButton;
    btnCancel: TButton;
    btnAdd: TRzBitBtn;
    btnDel: TRzBitBtn;
    Label1: TLabel;
    lblCurrentLib: TLabel;
    odLib: TOpenDialog;
    vi: TRzVersionInfo;
    Dirs: TOBDirectories;
    siLang: TsiLang;
    procedure btnAddClick(Sender: TObject);
    procedure btnDelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure lvDllClick(Sender: TObject);
  private
    { Private declarations }
  public
    procedure AddToListView(AFile: string);
  end;

var
  FormLibMgr: TFormLibMgr;

implementation

uses
  frmMain;

{$R *.dfm}

procedure TFormLibMgr.AddToListView(AFile: string);
var
  item: TListItem;
begin
  if FileExists(AFile) then
  begin
      item := lvDll.Items.Add;
      item.Caption := 'sqlite3.dll';
      vi.FilePath := AFile;
      item.SubItems.Add(vi.FileVersion);
      item.SubItems.Add(AFile);
      formmain.Config.AddLib(AFile);
  end
  else
    FormMain.Config.DelLib(AFile);
end;

procedure TFormLibMgr.btnAddClick(Sender: TObject);
var
  item: TListItem;
  tmp: TListItem;
  i: Integer;
begin
  if not odLib.Execute then
    Exit;
  //  add library
  for i:=0 to lvDll.Items.Count -1 do
  // for tmp in lvDll.Items do
  begin
    tmp := lvDll.Items[i];
    if tmp.SubItems[1] = odLib.FileName then
      Exit;
  end;
  vi.FilePath := odLib.FileName;
  item := lvDll.Items.Add;
  item.Caption := ExtractFileName(odLib.FileName);
  item.SubItems.Add(vi.FileVersion);
  item.SubItems.Add(odLib.FileName);
  FormMain.Config.AddLib(odLib.FileName);
end;

procedure TFormLibMgr.btnDelClick(Sender: TObject);
var
  idx: Integer;
  item: TListItem;
begin
  //  delete library
  if lvDll.Items.Count = 1 then
  begin
    MessageBox(Handle,PChar(siLang.GetText('sCantDel')), PChar(formmain.siLang.GetText('sHint')), mb_ok or MB_ICONINFORMATION);
    Exit;
  end;  
  idx := lvDll.ItemIndex;
  item := lvDll.Items[idx];
  formmain.Config.DelLib(item.SubItems[1]);
  if lblCurrentLib.Caption = item.SubItems[1] then
    lblCurrentLib.Caption := lvDll.Items[0].SubItems[1];
  lvDll.Items.Delete(idx);

end;

procedure TFormLibMgr.FormCreate(Sender: TObject);
var
  fn: string;
  i: Integer;
begin
  siLang.ActiveLanguage := formmain.Config.Language;
  //  auto load librarys
  if FormMain.Config.libraryCount = 0 then
  begin
    fn := Dirs.SystemDirectory;
    if fn[Length(fn)] <> '\' then
      fn := fn + '\';
    fn := fn + 'sqlite3.dll';
    AddToListView(fn);
    fn := ExtractFilePath(ParamStr(0))+'sqlite3.dll';
    AddToListView(fn);
  end
  else
  begin
    for i := 0 to formmain.Config.libraryCount - 1 do
      AddToListView(FormMain.Config.Librarys[i]);
  end;  
end;

procedure TFormLibMgr.lvDllClick(Sender: TObject);
var
  fn: string;
  idx: Integer;
  item: TListItem;
begin
  if lvDll.Items.Count = 0 then
    Exit;
  idx:=lvDll.ItemIndex;
  if idx = -1 then
    Exit;
  item := lvDll.Items[idx];

  fn := item.SubItems[1];
  if UpperCase(fn) = UpperCase(ExtractFilePath(ParamStr(0))+'sqlite3.dll') then
    lblCurrentLib.Caption := ExtractFileName('sqlite3.dll')
  else
    lblCurrentLib.Caption := fn;
end;

end.
